package com.paic.mhis.hcpms.agreement.healthcareprovider.util;

import java.io.Serializable;
import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.paic.mhis.hcpms.agreement.healthcareprovider.dto.StandardScoreDTO;

/**
 * 
 * @author GUOSHUNXU092
 * @time 2014-12-10
 */
public class StandardScoreDTOComparator implements
		Comparator<StandardScoreDTO>, Serializable {

	@Override
	public int compare(StandardScoreDTO dto1, StandardScoreDTO dto2) {
		String scoreName = dto1.getScoreName();
		String scoreName2 = dto2.getScoreName();
		if (scoreName == null) {
			return 1;// 为空情况放最下面
		} else if (scoreName2 == null) {
			return -1;
		}
		int scoreOrder = getNumber(scoreName);
		int scoreOrder2 = getNumber(scoreName2);
		return scoreOrder - scoreOrder2;
	}

	/**
	 * 从输入字符串中截取开头的数字，如果输入串中未出现数字，将默认返回999
	 * 
	 * @param input
	 * @return 一个整数
	 */
	public int getNumber(String input) {

		Pattern p = Pattern.compile("(\\d+)");
		Matcher m = p.matcher(input);
		String number = null;
		if (m.find()) {
			number = m.group(1);
		}// not find ,number still is null,it will go to next else block
		int result = 0;
		if (StringUtils.isNotEmpty(number)) {
			try {
				result = Integer.parseInt(number);
			} catch (Exception e) {
				// do nothing
			}
		} else {
			result = 999;// 当未找到序号，加个默认值使其排最下面
		}
		return result;

	}

}
